﻿package org.xhs.report.doc.model
{
    import __AS3__.vec.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.service.*;
    import baidu.wenku.common.ui.panel.*;
    import baidu.wenku.common.utils.*;
    import baidu.wenku.doc.*;
    import baidu.wenku.doc.ui.*;
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;

    public class DocContentModel extends EventDispatcher
    {
        protected var _pageth:int = 0;
        protected var _pagesLoaded:int = 0;
        protected var _pagesLoaderArray:Vector.<Loader>;
        protected var _pagesArray:Vector.<BlankPage>;
        protected var _inLoading:Boolean;
        protected var _firstLoading:Boolean = true;
        protected var _timeStartLoading:Number;
        protected var _delayPreLoadID:int;
        protected var _loadingTout:uint;
        protected var oriPageWidth:Number = 681;
        protected var oriPageHeight:Number = 665;
        protected var pageHeightOffset:Number = 10;
        protected var storeModel:StoreModel;
        protected var screenModel:ScreenModel;
        protected var storePage:StorePage;
        protected var loaderInfoVo:LoaderInfoVO;
        protected var docServiceModel:DocServiceModel;

        public function DocContentModel(param1:IEventDispatcher = null)
        {
            super(param1);
            this.initModel();
            this.initProvider();
            return;
        }// end function

        protected function initModel() : void
        {
            this.storeModel = InstanceManager.createSingletonInstance(StoreModel);
            this.loaderInfoVo = InstanceManager.createSingletonInstance(LoaderInfoVO);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.oriPageWidth = this.screenModel.stageWidth - DocConfig.DV_WIDTH_LEFT_PADDING - DocConfig.DV_WIDTH_RIGHT_PADDING - Config.DV_WIDTH_RIGHT_SCROLLBAR;
            this.oriPageHeight = this.screenModel.stageHeight - Config.TB_HEIGHT - Config.SB_HEIGHT;
            return;
        }// end function

        protected function initProvider() : void
        {
            this.docServiceModel = InstanceManager.createSingletonInstance(DocServiceModel);
            this.docServiceModel.addEventListener(PageServiceEvent.DOCINFO_LOADED, this.docInfoLoaded);
            this.docServiceModel.addEventListener(PageServiceEvent.START_LOADING, this.onStartLoading);
            this.docServiceModel.addEventListener(PageServiceEvent.PAGE_LOADED, this.pageLoaded);
            this.docServiceModel.addEventListener(PageServiceEvent.PAGE_FAILED, this.pageFailed);
            this.docServiceModel.addEventListener(PageServiceEvent.FILE_DELETED, this.fileDeleted);
            this.docServiceModel.addEventListener(PageServiceEvent.FILE_NO_DATA, this.fileNoData);
            this.docServiceModel.addEventListener(PageServiceEvent.FIRST_PARSE_BYTE, this.firstParseByte);
            this.docServiceModel.addEventListener(PageServiceEvent.PARSE_FAILED, this.onParseFailed);
            this.docServiceModel.docUrl = this.loaderInfoVo.docUrl;
            this.docServiceModel.docId = this.loaderInfoVo.docId;
            this.docServiceModel.docNpn = this.loaderInfoVo.docNpn;
            this.docServiceModel.isStore = this.storeModel.isStore;
            this.docServiceModel.storeFreePages = this.storeModel.storeFreePages;
            this.docServiceModel.storeCookie = this.storeModel.storeCookie;
            return;
        }// end function

        protected function docInfoLoaded(event:PageServiceEvent) : void
        {
            this.docServiceModel.removeEventListener(PageServiceEvent.DOCINFO_LOADED, this.docInfoLoaded);
            dispatchEvent(event);
            return;
        }// end function

        protected function onStartLoading(event:PageServiceEvent) : void
        {
            if (this._firstLoading)
            {
                dispatchEvent(event);
            }
            return;
        }// end function

        protected function pageLoaded(event:PageServiceEvent) : void
        {
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_7:BlankPage = null;
            var _loc_8:Shape = null;
            var _loc_9:Graphics = null;
            var _loc_2:* = event.pageth;
            var _loc_10:String = this;
            var _loc_11:* = this._pagesLoaded + 1;
            _loc_10._pagesLoaded = _loc_11;
            var _loc_3:* = this.docServiceModel.getaPageLoader(_loc_2);
            if (_loc_3 is Loader)
            {
                if (!this._pagesLoaderArray)
                {
                    this._pagesLoaderArray = new Vector.<Loader>(this.docServiceModel.pagesAllAvailable);
                }
                this._pagesLoaderArray[this.docServiceModel.pagethLoading] = Loader(_loc_3);
            }
            if (_loc_3 && _loc_3.width && _loc_3.height)
            {
                _loc_5 = this.oriPageWidth / _loc_3.width;
                _loc_6 = (this.oriPageHeight - Config.DV_WIDTH_RIGHT_SCROLLBAR) / _loc_3.height;
                if (this.loaderInfoVo.isPPT)
                {
                    if (_loc_5 > _loc_6)
                    {
                        _loc_4 = _loc_6;
                    }
                    else
                    {
                        _loc_4 = _loc_5;
                    }
                }
                else
                {
                    _loc_4 = _loc_5;
                }
                _loc_3.width = _loc_3.width * _loc_4;
                _loc_3.height = _loc_3.height * _loc_4;
                if (this.docServiceModel.pagethLoading == 0)
                {
                    Config.pageWidth = _loc_3.width;
                    Config.pageHeight = _loc_3.height;
                    this.addBlankPages();
                }
                _loc_7 = this._pagesArray[this.docServiceModel.pagethLoading] as BlankPage;
                if (this.storeModel.isStore && _loc_7 == this.storePage)
                {
                    return;
                }
                if (_loc_7)
                {
                    _loc_7.clearChildren();
                    _loc_7.addChild(_loc_3);
                    _loc_8 = new Shape();
                    _loc_9 = _loc_8.graphics;
                    _loc_9.lineStyle(0, 12111302);
                    _loc_9.drawRect(0, 0, _loc_7.width, _loc_7.height);
                    _loc_7.addChild(_loc_8);
                    _loc_7.mouseChildren = false;
                    _loc_7.mouseEnabled = false;
                    if (Math.abs(_loc_7.height - Config.pageHeight) >= this.pageHeightOffset)
                    {
                        dispatchEvent(new PageServiceEvent(PageServiceEvent.PAGE_REARRANGE));
                    }
                }
                if (this._firstLoading)
                {
                    this._firstLoading = false;
                    dispatchEvent(new PageServiceEvent(PageServiceEvent.BLANKPAGE_ADDED));
                }
                dispatchEvent(event);
            }
            return;
        }// end function

        protected function pageFailed(event:PageServiceEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        protected function fileDeleted(event:PageServiceEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        protected function fileNoData(event:PageServiceEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        protected function firstParseByte(event:PageServiceEvent) : void
        {
            dispatchEvent(event);
            return;
        }// end function

        protected function onParseFailed(event:PageServiceEvent) : void
        {
            if (event)
            {
                dispatchEvent(event);
            }
            return;
        }// end function

        protected function addBlankPages() : void
        {
            var _loc_4:BlankPage = null;
            var _loc_5:Boolean = false;
            var _loc_1:int = 0;
            var _loc_2:* = this.storeModel.isStore && this.storeModel.hasRecom ? ((this.docServiceModel.pagesAllAvailable - 1)) : (this.docServiceModel.pagesAllAvailable);
            this._pagesArray = new Vector.<BlankPage>(_loc_2);
            var _loc_3:* = _loc_1;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = new BlankPage();
                _loc_4.pageth = _loc_3 + 1;
                _loc_4.setSize(Config.pageWidth, Config.pageHeight);
                _loc_4.mouseChildren = false;
                this._pagesArray[_loc_3] = _loc_4;
                _loc_1++;
                _loc_3++;
            }
            if (this.storeModel.isStore)
            {
                _loc_5 = true;
                if (this.storeModel.isFree && !this.storeModel.hasRecom)
                {
                    _loc_5 = false;
                }
                if (_loc_5)
                {
                    this.storePage = new StorePage();
                    this.storePage.pageth = _loc_3 + 1;
                    this.storePage.init();
                    this._pagesArray[_loc_3] = this.storePage;
                }
            }
            return;
        }// end function

        protected function preLoad() : void
        {
            var tn:int;
            if (this._pagesLoaded >= this.docServiceModel.pagesAllAvailable && this.docServiceModel.pagesAllAvailable > 0)
            {
                if (this._delayPreLoadID)
                {
                    clearInterval(this._delayPreLoadID);
                }
                return;
            }
            var num:* = this._pageth;
            var pn:* = Math.floor(num / 5) * 5;
            var currentIndex:* = Math.max(0, pn);
            var loadCurrentGroup:Boolean;
            while (currentIndex < Math.min(pn + this.loaderInfoVo.docNpn, this.docServiceModel.pagesAllTotally))
            {
                
                if (!this.docServiceModel.isPagethLoaded(currentIndex))
                {
                    loadCurrentGroup;
                    break;
                }
                currentIndex = (currentIndex + 1);
            }
            if (!loadCurrentGroup)
            {
                if (num % 5 < 2)
                {
                    pn = pn - this.loaderInfoVo.docNpn;
                }
                else
                {
                    pn = pn + this.loaderInfoVo.docNpn;
                }
            }
            pn = Math.max(0, pn);
            tn = Math.min(pn + this.loaderInfoVo.docNpn, this.storeModel.isStore ? ((this.docServiceModel.pagesAllTotally - 1)) : (this.docServiceModel.pagesAllTotally));
            var pnReady:Boolean;
            var tnReady:Boolean;
            var i:int;
            var iLen:* = tn - pn;
            while (i < iLen)
            {
                
                if (!pnReady && !this.docServiceModel.isPagethLoaded(pn + i))
                {
                    pnReady;
                    pn = pn + i;
                }
                if (!tnReady && !this.docServiceModel.isPagethLoaded((tn - 1) - i))
                {
                    tnReady;
                    tn = tn - i;
                }
                if (pnReady && tnReady)
                {
                    break;
                }
                i = (i + 1);
            }
            if (pnReady && pnReady && pn < tn && pn < this.docServiceModel.pagesAllAvailable)
            {
                if (Math.floor(this.docServiceModel.docFpth / 5) == Math.floor(pn / 5))
                {
                    return;
                }
                this.docServiceModel.stopLoad();
                this.docServiceModel.docFpth = pn;
                try
                {
                    WenkuLog.addLog(WenkuLog.LOGURL_LOADDATA, "", 2);
                    this._timeStartLoading = new Date().time;
                    this.docServiceModel.loadData();
                    this._inLoading = true;
                    if (this._loadingTout)
                    {
                        clearInterval(this._loadingTout);
                    }
                    this._loadingTout = setInterval(function () : void
            {
                clearInterval(_loadingTout);
                _inLoading = false;
                return;
            }// end function
            , 8000);
                }
                catch (err:Error)
                {
                    _inLoading = false;
                    Logger.error("preLoad, load binary data error in:" + loaderInfoVo.docUrl);
                }
            }
            return;
        }// end function

        public function loadData() : void
        {
            try
            {
                WenkuLog.addLog(WenkuLog.LOGURL_LOADDATA, "", 2);
                this._timeStartLoading = new Date().time;
                this.docServiceModel.loadData();
                this._inLoading = true;
                if (this._loadingTout)
                {
                    clearInterval(this._loadingTout);
                }
                this._loadingTout = setInterval(function () : void
            {
                clearInterval(_loadingTout);
                _inLoading = false;
                return;
            }// end function
            , 8000);
                this._delayPreLoadID = setInterval(this.preLoad, 1000);
            }
            catch (err:Error)
            {
                _inLoading = false;
                Logger.error("loadData, load binary data error in:" + loaderInfoVo.docUrl);
            }
            return;
        }// end function

        public function get pagesArray() : Vector.<BlankPage>
        {
            return this._pagesArray;
        }// end function

        public function get pagesLoaderArray() : Vector.<Loader>
        {
            return this._pagesLoaderArray;
        }// end function

        public function get pagesLoaded() : int
        {
            return this._pagesLoaded;
        }// end function

        public function get pagesAllAvailable() : int
        {
            return this.docServiceModel.pagesAllAvailable;
        }// end function

        public function get pagesAllTotally() : int
        {
            return this.docServiceModel.pagesAllTotally;
        }// end function

        public function isPagethLoaded(param1:int) : Boolean
        {
            return this.docServiceModel.isPagethLoaded(param1);
        }// end function

        public function get pagethLoading() : int
        {
            return this.docServiceModel.pagethLoading;
        }// end function

        public function set pageth(param1:int) : void
        {
            this._pageth = param1;
            return;
        }// end function

        public function get docFpth() : int
        {
            return this.docServiceModel.docFpth;
        }// end function

        public function set docFpth(param1:int) : void
        {
            this.docServiceModel.docFpth = param1;
            return;
        }// end function

    }
}
